<template>
  <el-dialog
    title="上传文件"
    :close-on-click-modal="false"
    @close="closeHandle"
    :visible.sync="visible">
    <el-upload
      drag
      :action="url"
      :before-upload="beforeUploadHandle"
      :on-success="successHandle"
      :data="dataForm"
      :files-list="filesList"
      :multiple=true
      name="files"
      ref=“upload”
      accept=".jmx,.csv"
      style="text-align: center;
      ">
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      <div class="el-upload__tip" slot="tip">只支持jmx、csv格式的文件！</div>
    </el-upload>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      url: '',
      methods: '',
      data: {},
      num: 0,
      successNum: 0,
      filesList: [],
      dataForm: {
        caseIds: 0,
        files_id: 0
      }
    }
  },

  methods: {

    init(caseId) {
      this.url = this.$http.adornUrl(`/test/stress/upload?token=${this.$cookie.get('token')}`)
      this.methods = 'post';
      this.visible = true;
      console.log('caseIds', caseId);
      this.dataForm.caseIds = caseId
      this.data = this.$http.adornData(
        this.dataForm
      )
      console.log(this.dataForm);

    },
    // 上传之前
    beforeUploadHandle(file) {
      let testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
      const extension = testmsg === 'csv'
      const extension2 = testmsg === 'jmx'
      if (!extension && !extension2) {
        this.$message({
          message: '上传文件只能是 xls、xlsx格式!',
          type: 'warning'
        });
        return false
      }
      this.num++
    }
    ,
    // 上传成功
    successHandle(response, files, filesList) {

      this.filesList = filesList
      this.successNum++
      if (response && response.code === 0) {
        if (this.num === this.successNum) {
          this.$confirm('上传成功, 是否继续操作?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).catch(() => {
            this.visible = false
          })

        }
      } else {
        this.$message.error(response.msg)
      }
    }
    ,
    // 弹窗关闭时
    closeHandle() {
      this.filesList = []
      this.$emit('refreshDataList')
    }
  },

}
</script>
